package com.quanfaqiang.demo1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.quanfaqiang.demo1.mapper.*;
import com.quanfaqiang.demo1.pojo.*;
import com.quanfaqiang.demo1.service.StatisticalAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

@Service
public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisService {

    @Autowired
    private FurnitureMapper furnitureMapper;
    @Autowired
    private FurnituretypesMapper furnituretypesMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private QuyutypesMapper quyutypesMapper;
    @Autowired
    private StatetypesMapper statetypesMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public HashMap<String, Long> SelectAll() {
        QueryWrapper<Furniture> furnitureQueryWrapper = new QueryWrapper<>();
        Long furniture = furnitureMapper.selectCount(furnitureQueryWrapper);

        QueryWrapper<Furnituretypes> furnituretypesQueryWrapper = new QueryWrapper<>();
        Long furnituretypes = furnituretypesMapper.selectCount(furnituretypesQueryWrapper);

        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        Long notice = noticeMapper.selectCount(noticeQueryWrapper);

        QueryWrapper<Quyutypes> quyutypesQueryWrapper = new QueryWrapper<>();
        Long quyutypes = quyutypesMapper.selectCount(quyutypesQueryWrapper);

        QueryWrapper<Statetypes> statetypesQueryWrapper = new QueryWrapper<>();
        Long statetypes = statetypesMapper.selectCount(statetypesQueryWrapper);


        QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
        Long task = taskMapper.selectCount(taskQueryWrapper);

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        Long user = userMapper.selectCount(userQueryWrapper);


        HashMap<String, Long> stringLongHashMap = new HashMap<>();
        stringLongHashMap.put("家具", furniture);
        stringLongHashMap.put("家具类型", furnituretypes);
        stringLongHashMap.put("通知", notice);
        stringLongHashMap.put("区域类型", quyutypes);
        stringLongHashMap.put("状态类型", statetypes);
        stringLongHashMap.put("家具任务", task);
        stringLongHashMap.put("用户", user);


//        List<Long> objects = new ArrayList<>();
//        objects.add(furniture);
//        objects.add(furnituretypes);
//        objects.add(notice);
//        objects.add(quyutypes);
//        objects.add(statetypes);
//        objects.add(task);
//        objects.add(user);
        return stringLongHashMap;
    }

    @Override
    public TreeMap<LocalDate, Long> UserCount() {
//        HashMap<String, Long> result = new HashMap<>();
//        LocalDateTime yesterday = LocalDateTime.now().minusDays(1).truncatedTo(ChronoUnit.DAYS);
//        LocalDateTime today = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
//        System.out.println(yesterday + ":" + yesterday.plusDays(1));
//        System.out.println(today + ":" + today.plusDays(1));
//        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//        userQueryWrapper.ge("add_time", yesterday);
//        userQueryWrapper.le("add_time", yesterday.plusDays(1));
//        Long addTime = userMapper.selectCount(userQueryWrapper);
//
//        QueryWrapper<User> userQueryWrapper1 = new QueryWrapper<>();
//        userQueryWrapper1.ge("add_time", today);
//        userQueryWrapper1.le("add_time", today.plusDays(1));
//        Long addTime1 = userMapper.selectCount(userQueryWrapper1);
//
//
//        result.put("yesterday", addTime1 - addTime);
//        result.put("today", addTime1);


        TreeMap<LocalDate, Long> localDateTimeLongHashMap = new TreeMap<>((a, b) -> b.compareTo(a));
        LocalDateTime today = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS);
        Duration between = Duration.between(getMinDate(), today);
        long between1 = ChronoUnit.DAYS.between(getMinDate(), today);
        System.out.println(between1+"]]]]]]]]]]]]]]]]");
        for (int i = 0; i <= between1+1; i++) {
            LocalDateTime day = LocalDateTime.now().minusDays(i).truncatedTo(ChronoUnit.DAYS);
            System.out.println(day);
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.ge("add_time", day);
            userQueryWrapper.le("add_time", day.plusDays(1));
            Long addTime = userMapper.selectCount(userQueryWrapper);

            LocalDate localDate = day.toLocalDate();
            localDateTimeLongHashMap.put(localDate, addTime);
        }
        System.out.println(getMinDate()+":"+getMaxDate());


        return localDateTimeLongHashMap;
    }
    public LocalDateTime getMinDate() {
        QueryWrapper<User> wrapper = Wrappers.query();
        // 使用 MyBatis-Plus 的 MIN 函数获取最小日期
        wrapper.select("MIN(add_time) as minDate");

        // 执行查询
        User user = userMapper.selectOne(wrapper);

        List<LocalDateTime> result = userMapper.selectObjs(wrapper);
        System.out.println(result+"////////////");

        if (result != null && !result.isEmpty()) {
            // 获取最大日期，假设返回的对象是 Date 类型
            return  result.get(0);
        }
        return null;
    }


    public LocalDateTime getMaxDate() {
        QueryWrapper<User> wrapper = Wrappers.query();
        // 使用 MyBatis-Plus 的 MAX 函数获取最大日期
        wrapper.select("MAX(add_time) as maxDate");

        // 执行查询
        User user = userMapper.selectOne(wrapper);
        System.out.println(user+"===================");
        List<LocalDateTime> result = userMapper.selectObjs(wrapper);
        System.out.println(result+"////////////");

        if (result != null && !result.isEmpty()) {
            // 获取最大日期，假设返回的对象是 Date 类型
            return result.get(0);
        }
        return null;
    }

}
